//
//  FavorArticleViewController.m
//  Adorable_Things
//
//  Created by lanou on 16/1/20.
//  Copyright © 2016年 Banana Inc. All rights reserved.
//

#import "FavorGoodsViewController.h"
#import "ZYCategorySecondCollectionViewCell.h"
#import "DJCommentListModel.h"
#import "ATMainDetailViewController.h"

@interface FavorGoodsViewController () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation FavorGoodsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _dataArr = [NSMutableArray array];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 15, 10, 15);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.itemSize = CGSizeMake((kWidth - 40) / 2, 300);
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"ZYCategorySecondCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"ZYCategorySecondCollectionViewCell"];
    [self.view addSubview:self.collectionView];
    
    [self setUpData];
}

- (void)setUpData
{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"auth_token"] = [ATUserManager shareManager].auth_token;
    parDic[@"page"] = @"0";
    parDic[@"page_size"] = @"20";
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:@"http://api.menghuoapp.com/v1/user/favorite_item" parameters:parDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *dataArr = responseObject[@"data"];
        for (NSDictionary *oneDic in dataArr) {
            DJCommentListModel *model = [[DJCommentListModel alloc] init];
            [model setValuesForKeysWithDictionary:oneDic];
            [_dataArr addObject:model];
        }
        [self.collectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败");
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZYCategorySecondCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZYCategorySecondCollectionViewCell" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"ZYCategorySecondCollectionViewCell" owner:self options:nil] lastObject];
    }
    DJCommentListModel *model = _dataArr[indexPath.row];
    cell.model = (ZYCategorySecondPageModel *)model;
    cell.likeImageView.hidden = YES;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DJCommentListModel *model = _dataArr[indexPath.row];
    ATMainDetailViewController *categoryVC = [[ATMainDetailViewController alloc] init];
    self.hidesBottomBarWhenPushed = YES;
    categoryVC.item_id = model.item_id;
    [self.navigationController pushViewController:categoryVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
